/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Prestahop_retrievement;

import com.insa.a1.hibernate.ps.PsAddress;
import com.insa.a1.hibernate.ps.PsCustomer;
import com.insa.a1.hibernate.ps.PsOrders;
import com.insa.a1.xmladapter.DateAdapter;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

/**
 *
 * @author strokyl
 */
@Entity
public class Customer implements Serializable {

    @Id
    private int idCustomer;
    
    private char gender;
    private String firstName;
    private String lastName;
    private String email;
    private String company;

    @Temporal(TemporalType.DATE)
    private Date bithday;

    @Temporal(TemporalType.TIMESTAMP)
    @XmlJavaTypeAdapter(DateAdapter.class)
    private Date dateUppAdd;

    @OneToMany(cascade=CascadeType.ALL,  mappedBy="customer")
    private Set<Address> addresses;

    @OneToMany(cascade=CascadeType.ALL, mappedBy="customer")
    private Set<Orders> orders;

    @XmlTransient
    @Transient
    private Date lastUpdateDate;

    public Customer() {
    }

    public Date getLastUpdateDate() {

	    if(lastUpdateDate == null)  {
		    lastUpdateDate = dateUppAdd;
		    Date candidate;
		    for(Orders o : orders) {
			   candidate = o.getLastUpdateDate(); 
			   if(candidate.after(lastUpdateDate)) {
				   lastUpdateDate = candidate;
			   }
		    }

		    for(Address a : addresses) {
			   candidate = a.getLastUpdateDate(); 
			   if(candidate.after(lastUpdateDate)) {
				   lastUpdateDate = candidate;
			   }
		    }
	    }

	    return lastUpdateDate;
    }

    public Customer(PsCustomer ps) {
        this.gender = ps.getGender() == null || ps.getGender().getType() ? 'M':'F';
        this.idCustomer = ps.getIdCustomer();
        this.firstName = ps.getFirstname();
        this.lastName = ps.getLastname();
        this.email = ps.getEmail();
        this.company = ps.getCompany();
        this.bithday = ps.getBirthday();
        this.dateUppAdd = ps.getDateUpd();

        addresses = new HashSet<Address>();

        for(PsAddress psA : ps.getAddresses()) {
            addresses.add(new Address(psA, this));
        }


        orders = new HashSet<Orders>();

        for(PsOrders psO : ps.getOrders()) {
            orders.add(new Orders(psO, this));
        }
        
    }

    public Set<Address> getAddresses() {
        return addresses;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public void setAddresses(Set<Address> addresses) {
        this.addresses = addresses;
    }

    public Date getBithday() {
        return bithday;
    }

    public void setBithday(Date bithday) {
        this.bithday = bithday;
    }

    public Date getDateUppAdd() {
        return dateUppAdd;
    }

    public void setDateUppAdd(Date dateUppAdd) {
        this.dateUppAdd = dateUppAdd;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public char  getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }

    public int getIdCustomer() {
        return idCustomer;
    }

    public void setIdCustomer(int idCustomer) {
        this.idCustomer = idCustomer;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Set<Orders> getOrders() {
        return orders;
    }

    public void setOrders(Set<Orders> orders) {
        this.orders = orders;
    }


    @Override
    public String toString() {
        return String.format("Customer( %s , %s )", firstName, lastName);
    }
}
